﻿namespace EyeOpen.Services
{
	using System;
	using System.Linq;
	using EyeOpen.Data;
	using EyeOpen.Domain;

	public class WebSiteService : IWebSiteService
	{
		public IQueryable<ContentLocalized> GetContentLocalizedList()
		{
			using (var dbContext = CreateUow())
			{
				return
					dbContext
						.ContentLocalizedRepository
						.Include("Language")
						.ToList()
						.AsQueryable();
			}
		}

		/// <exception cref="Exception"><c>Exception</c>.</exception>
		public void Update(ContentLocalized contentLocalized)
		{
			using (var dbContext = CreateUow())
			{
				var originalContentLocalized =
					dbContext
						.ContentLocalizedRepository
						.Include("Content")
						.Include("Language")
						.Single(c => c.Id == contentLocalized.Id);

				originalContentLocalized
					.Title = contentLocalized.Title;
				originalContentLocalized
					.Description = contentLocalized.Description;
				originalContentLocalized
					.Keywords = contentLocalized.Keywords;
				originalContentLocalized
					.Body = contentLocalized.Body;

				dbContext.SaveChanges();
			}
		}

		private WebSiteDbContext CreateUow()
		{
			return new WebSiteDbContext("WebSiteConnection");
		}
	}
}